package com.syp.jrx.batch.process;

import com.syp.jrx.batch.entity.entity;
import com.syp.jrx.model.Statistics;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;

@Service
public class PersonItemProcessor implements ItemProcessor<ArrayList<entity>, Statistics> {

    private static SimpleDateFormat f =
            new SimpleDateFormat("yyyyMMdd");
    @Override
    public Statistics process(ArrayList<entity> entities) throws Exception {
        Statistics statistics =new Statistics();
        int cust_id = -1;
        String surname = null;
        BigDecimal tran_max_amt= BigDecimal.valueOf(0);
        BigDecimal pay_amt = BigDecimal.valueOf(0);
        int tran_cnt = 0;
        int pay_cnt =0;
        BigDecimal tran_amt = BigDecimal.valueOf(0);
        for (entity tity:entities
             ) {
            surname=tity.getSurname();
            cust_id=tity.getCust_id();
            if(tity.getTrans_type()=="消费"){
                if(tran_max_amt.compareTo(tity.getBill()) == -1 ){
                    tran_max_amt=tity.getBill();
                }
                tran_cnt++;
            }else {
                if(tran_max_amt.compareTo(tity.getBill()) == -1 ){
                    tran_max_amt=tity.getBill();
                }
                pay_amt.add(tity.getBill());
                pay_cnt++;
            }
            tran_amt.add(tity.getBill());
        }
        System.out.println(cust_id);
        if(cust_id != -1){
            statistics.setS_index("uuid_"+f.format(new Date()));
            statistics.setCust_id(cust_id);
            statistics.setUpdate_time(LocalDateTime.now());
            statistics.setTrans_date(LocalDate.now());
            statistics.setSurname(surname);
            statistics.setTran_max_amt(tran_max_amt);
            statistics.setPay_amt(pay_amt);
            statistics.setTran_cnt(tran_cnt);
            statistics.setPay_cnt(pay_cnt);
            statistics.setTran_amt(tran_amt);
            return statistics;
        }else{
            return null;
        }
    }
}
